<template>
  <div class="register-page">
    <!-- 页面头部 -->
    <header class="register-header">
      <div class="header-content">
        <router-link to="/" class="logo">
          <h1>小红书</h1>
        </router-link>
        <div class="header-actions">
          <span>已有账号？</span>
          <router-link to="/login" class="login-link">
            立即登录
          </router-link>
        </div>
      </div>
    </header>

    <!-- 主要内容区域 -->
    <main class="register-main">
      <div class="register-container">
        <div class="register-card">
          <h2 class="register-title">注册小红书账号</h2>
          <p class="register-subtitle">开启美好生活之旅</p>

          <!-- 注册表单 -->
          <el-form
            :model="formData"
            :rules="formRules"
            ref="formRef"
            @keyup.enter="handleRegister"
            class="register-form"
          >
            <el-form-item prop="username">
              <el-input
                v-model="formData.username"
                placeholder="请输入用户名"
                size="large"
                :prefix-icon="User"
              />
            </el-form-item>
            
            <el-form-item prop="email">
              <el-input
                v-model="formData.email"
                placeholder="请输入邮箱"
                size="large"
                :prefix-icon="Message"
              />
            </el-form-item>
            
            <!-- <el-form-item prop="verificationCode">
              <div class="verification-code">
                <el-input
                  v-model="formData.verificationCode"
                  placeholder="请输入验证码"
                  size="large"
                  class="verification-input"
                />
                <el-button
                  :loading="sending"
                  :disabled="sending || !formData.email"
                  @click="handleSendCode"
                >
                  {{ sendButtonText }}
                </el-button>
              </div>
            </el-form-item> -->
            
            <el-form-item prop="password">
              <el-input
                v-model="formData.password"
                type="password"
                placeholder="请输入密码"
                size="large"
                show-password
                :prefix-icon="Lock"
              />
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input
                v-model="formData.confirmPassword"
                type="password"
                placeholder="请确认密码"
                size="large"
                show-password
                :prefix-icon="Lock"
              />
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                size="large"
                :loading="loading"
                @click="handleRegister"
                class="register-btn"
              >
                {{ loading ? '注册中...' : '注册' }}
              </el-button>
            </el-form-item>
          </el-form>

          <!-- 用户协议 -->
          <div class="agreement">
            <p class="agreement-text">
              注册即表示您同意小红书的
              <a href="#" class="agreement-link">用户协议</a>
              和
              <a href="#" class="agreement-link">隐私政策</a>
            </p>
          </div>
        </div>
      </div>
    </main>

    <!-- 页面底部 -->
    <footer class="register-footer">
      <div class="footer-links">
        <a href="#">关于我们</a>
        <a href="#">帮助中心</a>
        <a href="#">隐私政策</a>
        <a href="#">用户协议</a>
      </div>
      <p class="copyright">© 2023 小红书. All rights reserved.</p>
    </footer>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Message } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user'

const router = useRouter()
const userStore = useUserStore()

// 表单引用和状态
const formRef = ref()
const loading = ref(false)

// 移除验证码相关状态
const sending = ref(false)
const countdown = ref(0)
const sendButtonText = ref('发送验证码')

// 表单数据 - 移除 verificationCode
const formData = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

// 表单验证规则 - 移除 verificationCode
const formRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
}


// 处理注册 - 移除验证码相关
const handleRegister = async () => {
  if (!formRef.value) return
  
  try {
    const valid = await formRef.value.validate()
    if (!valid) return
    
    loading.value = true
    
    const registerData = {
      nickname: formData.username,
      email: formData.email,
      password: formData.password,
    }
    await userStore.userRegister(registerData)
   
    ElMessage.success('注册成功，请登录')
    router.push('/login')
  } catch (error) {
    const errorMsg = error.response?.data?.message || '注册失败，请稍后重试'
    // ElMessage.error(errorMsg)
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f8f8f8;
  
  .register-header {
    background: #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
    
    .header-content {
      max-width: 1200px;
      margin: 0 auto;
      padding: 0 20px;
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      
      .logo {
        color: #ff2e4d;
        text-decoration: none;
        
        h1 {
          margin: 0;
          font-size: 24px;
          font-weight: bold;
        }
      }
      
      .header-actions {
        display: flex;
        align-items: center;
        gap: 10px;
        color: #666;
        font-size: 14px;
        
        .login-link {
          color: #ff2e4d;
          font-weight: 500;
          
          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
  }
  
  .register-main {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
  }
  
  .register-container {
    width: 100%;
    display: flex;
    justify-content: center;
  }
  
  .register-card {
    background: #fff;
    padding: 40px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    width: 400px;
    
    .register-title {
      text-align: center;
      margin-bottom: 10px;
      color: #333;
      font-size: 24px;
      font-weight: bold;
    }
    
    .register-subtitle {
      text-align: center;
      margin-bottom: 30px;
      color: #666;
      font-size: 14px;
    }
    
    .register-form {
      margin-bottom: 20px;
      
      .verification-code {
        display: flex;
        gap: 10px;
        
        .verification-input {
          flex: 1;
        }
        
        .el-button {
          width: 120px;
          white-space: nowrap;
        }
      }
    }
    
    .register-btn {
      width: 100%;
      margin-top: 10px;
      height: 44px;
      font-size: 16px;
      background-color: #ff2e4d;
      border-color: #ff2e4d;
      
      &:hover {
        background-color: #ff4d64;
        border-color: #ff4d64;
      }
    }
    
    .agreement {
      text-align: center;
      margin-top: 20px;
      
      .agreement-text {
        color: #666;
        font-size: 12px;
        margin: 0;
        
        .agreement-link {
          color: #ff2e4d;
          
          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
  }
  
  .register-footer {
    padding: 30px 0;
    text-align: center;
    background: #fff;
    
    .footer-links {
      margin-bottom: 10px;
      
      a {
        color: #666;
        text-decoration: none;
        margin: 0 10px;
        font-size: 13px;
        
        &:hover {
          color: #ff2e4d;
        }
      }
    }
    
    .copyright {
      color: #999;
      font-size: 12px;
      margin: 0;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .register-page {
    .register-header {
      .header-content {
        padding: 0 15px;
        
        .logo h1 {
          font-size: 20px;
        }
        
        .header-actions {
          font-size: 13px;
        }
      }
    }
    
    .register-card {
      padding: 30px 20px;
      width: 100%;
      max-width: 350px;
    }
  }
}
</style>